{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 素数阶双线性群"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from charm.toolbox.pairinggroup import PairingGroup, G1, G2, GT, ZR, pair as e\n",
    "\n",
    "# 初始化\n",
    "group = PairingGroup('SS512')\n",
    "# group = PairingGroup('../pbc.conf.d/a.properties', param_file=True)\n",
    "print(group)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 元素随机选取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 单个选取\n",
    "e1 = group.random(G1)\n",
    "n1 = group.random(ZR)\n",
    "print(f'e1: {e1}\\nn1: {n1}')\n",
    "\n",
    "# 选取多个\n",
    "r1, r2, r3 = group.random(ZR, 3)\n",
    "print(f'r1: {r1}\\nr2: {r2}\\nr3: {r3}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ZR元素初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ini = group.init(ZR, 123)\n",
    "print(ini)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 生成元"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = group.random(G1)\n",
    "print(g)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 单位元\n",
    "$ nP = O $"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "unit = g ** group.order()\n",
    "# unit = g * group.init(ZR, group.order())\n",
    "print(unit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 双线性\n",
    "$e(u ^ x, v ^ y) = e(u, v) ^ {xy} $"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "u, v = group.random(G1, 2)\n",
    "x, y = group.random(ZR, 2)\n",
    "\n",
    "assert e(u ** x, v ** y) == e(u, v) ** (x * y), '双线性验证失败'\n",
    "print('双线性验证通过')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Hash值映射到群元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "e_hash = group.hash('hello world', G1)\n",
    "print(e_hash)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.9 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "e5030792b3492f6b12d94f1f48beca3d8e59ec05fd59d0aaaa48e684281ed297"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
